package com.example.fuluchou.dao.mapper;

import com.example.fuluchou.dao.dataobject.Encrypt;
import com.example.fuluchou.dao.dataobject.UserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select count(*) from user where email = #{email}")
    int countByMail(@Param("email") String mail);

    @Select("select count(*) from user where phone_number = #{phoneNumber}")
    int countByPhone(@Param("phoneNumber")Encrypt phoneNumber);

    /**
     *
     * @Options 将 userDO 插入 Mysql 后自动生成的 ID 写入到 userDO 的 id 中
     */
    @Insert("insert into user (user_name,email,phone_number,password,identity)" +
            " values (#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insert(UserDO userDO);

    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber")Encrypt phoneNumber);

    @Select("select * from user where email = #{email}")
    UserDO selectByMail(@Param("email")String email);

    @Select("<script>" +
            " select * from user" +
            " <if test=\"identity!=null\">" +
            "  where identity = #{identity}" +
            " </if>" +
            " order by id desc" +
            " </script>")
    List<UserDO> findUserInfo(@Param("identity")String identity);

    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection='list' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistByIds(@Param("list") List<Long> userIds);

    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item='item' collection='list' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("list") List<Long> collect);
}

























